package com.open.demo.mvvm.demo

import android.os.Bundle
import android.util.Log
import androidx.lifecycle.lifecycleScope
import com.open.demo.mvvm.Constants
import com.open.demo.mvvm.bean.Result
import com.open.demo.mvvm.engine.BaseMvvvmActivity
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

/**
 *
 * @Author: hailong.qiu hailong.qiu@xgimi.com
 * @Maintainer: hailong.qiu hailong.qiu@xgimi.com
 * @Date: 2022/2/17
 * @Copyright: 2022 www.xgimi.com Inc. All rights reserved.
 */
class MvvmTestActivity : BaseMvvvmActivity<SearchViewModel>() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        lifecycleScope.launch {
            viewModel.searchListState.collect { result ->
                when (result) {
                    is Result.Loading -> {
                        val str = result.data as String
                        Log.d(Constants.TAG, "getSearchList Loading str:${str}")
                    }
                    is Result.Success -> {
                        val datas: List<SearchData> = result.data as List<SearchData>
                        Log.d(Constants.TAG, "getSearchList Success datas size:${result.data.size}")
                    }
                    is Result.Failure -> {
                        Log.d(
                            Constants.TAG,
                            "getSearchList Failure errorCode:${result.code} msg:${result.data}"
                        )
                    }
                }
            }
        }
        viewModel.getSearchList("三国")
    }

}